package com.yb.ybms

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import com.yb.ybms.ui.theme.YbMessageServerTheme
import com.yb.ybmsgservice.bean.YbMessageBusChannel
import com.yb.ybmsgservice.compose.ScreenMask
import com.yb.ybmsgservice.mqtt.MQTTManager
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            YbMessageServerTheme {
                Box(modifier = Modifier.fillMaxSize()) {
                    Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                        Greeting(
                            name = "Android",
                            modifier = Modifier.padding(innerPadding)
                        )
                        LaunchedEffect(Unit) {
                            GlobalScope.launch {
                                YbMessageBusChannel.connectStateFlow.map {
                                    Log.e("===lcy===", "MQTT: ${it}")
                                }.collect()
                            }
                        }

                    }
                    ScreenMask()
                }

            }
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {

    val context = LocalContext.current


//    YbMessageService.rawDataChannel.receiveAsFlow().filter { c->
//        c.topic == ""
//    }.collectAsState(initial = null)
//
//    YbMessageService.rawDataChannel.receiveAsFlow().filter { c->
//        c.topic == ""
//    }.collectAsState(initial = null)

//    YbMessageBusChannel.filterTopic("waiting_area/publicity").mapData<EduBody>("data").map {
//        it
//    }.collectAsState(initial = null)

    LaunchedEffect(Unit) {
        YbMessageBusChannel.filterTopic("surgery/survey_all").map {
            it
        }.collect()
    }


    Column {


        Text(
            text = "启动服务",
            modifier = modifier.clickable {
                MQTTManager.getInstance().init(context, "tcp://10.10.15.40:1883")
            }
        )

        Text(
            text = "订阅mqtt",
            modifier = modifier.clickable {
                MQTTManager.getInstance().subScribe(true, Pair("surgery/survey_all", 0), Pair("waiting_area/publicity", 0), Pair("/cc/ol", 0))
            }
        )

        Text(
            text = "发送mqtt",
            modifier = modifier.clickable {
                MQTTManager.getInstance().publish("/cc/ol", "{\"TT\":\"vv\"}")
            }
        )

    }


}


data class EduBody(
    val body: List<EduInfo> = emptyList()
)

data class EduInfo(
    val content: String?,
    val educationTitle: String?,
    val educationType: String?,
    val eqId: Any?,
    val goLive: String?,
    val id: Long?,
    val jump: String?,
    val logo: String?,
    val marquee: String?,
    val operTime: String?,
    val spare: Any?,
    val spare1: Any?,
    val spare2: Any?,
    val video: String?
)